package com.kuqi.core;

import android.os.Bundle;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.kuqi.http.HttpCmd;
import com.kuqi.http.HttpHelper;

//定义我们自己的Activity,添加抽象方法
public abstract class AppActivity extends AppCompatActivity {

    // 用于缓存当前Activity的宽高等数据
    private int currentActivityHeight;
    private int currentActivityWidth;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        currentActivityWidth = dm.widthPixels;
        currentActivityHeight = dm.heightPixels;
    }

    //调用此方法隐藏虚拟导航栏
    public void hideVirtualKey(boolean touch_show){
        if(touch_show){
            // touch the screen, the navigation bar will show
            Window window = getWindow();
            WindowManager.LayoutParams params = window.getAttributes();
            params.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
            window.setAttributes(params);
        }else{
            // This work only for android 4.4+
            // hide navigation bar permanently in android activity
            // touch the screen, the navigation bar will not show
            int flags = View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
            getWindow().getDecorView().setSystemUiVisibility(flags);
        }
    }

    // 调用此方法实现状态栏的显示于隐藏
    public void hideStateTitle(boolean shown){
        if(shown){ // 展现
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
            getWindow().setAttributes(lp);
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        }else{ // 隐藏
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
            getWindow().setAttributes(lp);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        }
    }

    // 调用此方法实现页面标题栏的隐藏
    public void hideActionBar(){
        if(getSupportActionBar() != null)
            getSupportActionBar().hide();
    }


    public int getCurrentActivityWidth() {
        return currentActivityWidth;
    }

    public int getCurrentActivityHeight() {
        return currentActivityHeight;
    }
}
